using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameStateManagement;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Graphics;
using GameStateManagementSample.QuickMath;
namespace GameStateManagementSample.Screens
{
    class Screen_ChoseLevel : GameScreen
    {
        #region Fields
        ContentManager m_Content;
        InputAction pauseAction;
        
        Texture2D m_TexBack,m_TexChoose,m_TexCantChoose;
        SpriteFont m_Font;
        int m_UnlockLevel;
        #endregion
    #region Initialization
        public Screen_ChoseLevel()
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.0f);
            TransitionOffTime = TimeSpan.FromSeconds(1.0f);
            pauseAction = new InputAction(
                new Buttons[] { Buttons.Start, Buttons.Back },
                new Keys[] { Keys.Escape },
                true);
            
        }
        public override void Activate(bool instancePreserved)
        {
            if (!instancePreserved)
            {
                if (m_Content == null)
                {
                    m_Content = new ContentManager(ScreenManager.Game.Services, "Content");
                    ScreenManager.Game.ResetElapsedTime();
                    m_TexBack = m_Content.Load<Texture2D>(@"Image/choselevel");
                    m_TexChoose = m_Content.Load<Texture2D>(@"Image/Choose");
                    m_TexCantChoose = m_Content.Load<Texture2D>(@"Image/CantChoose");
                    m_Font = m_Content.Load<SpriteFont>("gamefont");
                    m_UnlockLevel = Level.Unlock;

                }
            }
        }
        public override void Deactivate()
        {
            base.Deactivate();
        }
        public override void Unload()
        {
            base.Unload();
        }
        #endregion
        #region Update and Draw
        protected virtual void OnCancel()
        {

        }
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, false);

            if (!otherScreenHasFocus)
            {

            }
        }
        public override void HandleInput(Microsoft.Xna.Framework.GameTime gameTime, InputState input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            PlayerIndex player;
            if (pauseAction.Evaluate(input, ControllingPlayer, out player))
            {
                foreach (GameScreen screen in ScreenManager.GetScreens())
                {
                    screen.ExitScreen();
                }
                ScreenManager.AddScreen(new Screen_Menu(), null);
            }
            TouchCollection touches = input.TouchState;

            foreach (TouchLocation touch in touches)
            {
                if (touch.State == TouchLocationState.Pressed)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        for (int i = 0; i < 7; i++)
                        {
                            if (j * 7 + i < m_UnlockLevel && TransitionPosition ==0)
                            {
                                Rectangle r = new Rectangle(100 * i + 60, 100 * j + 110, 80, 80);
                                if (r.Contains((int)touch.Position.X,(int)touch.Position.Y))
                                {
                                   
                                    foreach (GameScreen screen in ScreenManager.GetScreens())
                                    {
                                        screen.ExitScreen();
                                    }
                                    ScreenManager.AddScreen(new Screen_QuickMath(j * 7 + i+1), null);
                                    Level.Cur = j * 7 + i + 1;
                                }
                            }
                        }
                    }
                }
            }
        }

       
        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            Vector2 pos = new Vector2((TransitionPosition) * 800.0f, 0);
            spriteBatch.Begin();
            spriteBatch.Draw(m_TexBack, Vector2.Zero +pos, Color.White);
            for (int j = 0; j < 3;j++ )
            {
                for (int i = 0; i < 7;i++ )
                {
                    if (j * 7 + i < m_UnlockLevel)
                    {
                        spriteBatch.Draw(m_TexChoose, new Vector2(100 * i + 50, 100 * j + 100) + pos, Color.White);
                        spriteBatch.DrawString(m_Font, (j * 7 + i + 1).ToString(), new Vector2(100 * i + 50 + 25, 100 * j + 100 + 25) + pos, Color.Black);
                    } 
                    else
                    {
                        spriteBatch.Draw(m_TexCantChoose, new Vector2(100 * i + 50, 100 * j + 100) + pos, Color.White);
                        spriteBatch.DrawString(m_Font, (j * 7 + i + 1).ToString(), new Vector2(100 * i + 50 + 25, 100 * j + 100 + 25) + pos, Color.White);
                    }
                    
                    
                }
            }
            spriteBatch.End();
        }
        
        #endregion
    }
}
